package com.tyc.c12;

import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * 自定义参数解析器
 */
public class TokenArgumentResolver implements HandlerMethodArgumentResolver {

    /**
     * 是否解析某个参数
     */
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        // 判断是否有token注解
        Token token = methodParameter.getParameterAnnotation(Token.class);
        if(null == token){
            return false;
        }
        return true;
    }

    /**
     * 解析参数
     */
    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer,
                                  NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        return nativeWebRequest.getHeader("token");
    }
}
